package com.zyd.shiro.utils;


import com.zyd.shiro.entity.ArcFile;
import com.zyd.shiro.framework.object.AbstractDataDO;

import java.lang.reflect.Field;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ClassToSQLTool{
    public static final String STR_TYPE = "java.lang.String";

    public static final String INT_TYPE = "java.lang.Integer";



    public static void main(String[] args) {
        gnerSql(ArcFile.class);
    }

    public static void gnerSql(Class cls){
        String sql = "";
        Field[] fields = cls.getDeclaredFields();
        for(Field field:fields){
            String fieldName = field.getName();
            String type = field.getType().getName();
            System.out.println(type);
            System.out.println(type.equals(STR_TYPE.trim()));

            if(type.equals(STR_TYPE)){
                    sql +=" `"+camel4underline(fieldName)+"` varchar(200) DEFAULT NULL,\n";
            } else if(type.equals(INT_TYPE)){
                sql +=" `"+camel4underline(fieldName)+"` int DEFAULT NULL,\n";
            }else {
                sql +=" `"+camel4underline(fieldName)+"` bigint DEFAULT NULL,\n";
            }
        }
        System.out.println(sql);
    }

    public static String camel4underline(String param){
        Pattern p=Pattern.compile("[A-Z]");
        if(param==null ||param.equals("")){
            return "";
        }
        StringBuilder builder=new StringBuilder(param);
        Matcher mc=p.matcher(param);
        int i=0;
        while(mc.find()){
            builder.replace(mc.start()+i, mc.end()+i, "_"+mc.group().toLowerCase());
            i++;
        }

        if('_' == builder.charAt(0)){
            builder.deleteCharAt(0);
        }
        return builder.toString();
    }
}
